package cn.xing.managementSystem.service;

import cn.xing.managementSystem.bean.pageable.PageableConverter;
import cn.xing.managementSystem.bean.pageable.PageableRequest;
import cn.xing.managementSystem.bean.pageable.PageableResponse;
import cn.xing.managementSystem.domain.Course;
import cn.xing.managementSystem.domain.ImageStore;
import cn.xing.managementSystem.enums.CourseType;
import cn.xing.managementSystem.repository.CourseRepository;
import cn.xing.managementSystem.utils.Preconditions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * @author cuixing
 * @package cn.xing.managementSystem.service
 * @class CourseService
 * @email xing.cui@magicwindow.cn
 * @date 2018/4/24 上午10:48
 * @description
 */
@Service
@Transactional
public class CourseService {

    @Autowired
    private CourseRepository courseRepository;

    @Autowired
    private ImageStoreService imageStoreService;

    @Value("${image.boot.path}")
    private String imageBootPath;


    public Course findById(Long courseId) {
        return courseRepository.findByIdAndAndDeletedIsFalse(courseId);
    }


    public boolean saveCourseAndImageStore(Course course, Long courseImageId, MultipartFile courseImageFile) throws IOException {
        if(Preconditions.isBlank(courseImageId) && Preconditions.isNotBlank(courseImageFile)){
            String originalFilename = courseImageFile.getOriginalFilename();
            String fileFormat = originalFilename.substring(originalFilename.lastIndexOf("."), originalFilename.length());
            String imagePath = imageBootPath + UUID.randomUUID().toString() + fileFormat;
            File tempFile = new File(imagePath);
            if(!tempFile.exists()){
                tempFile.createNewFile();
            }
            courseImageFile.transferTo(tempFile);
            ImageStore imageStore = new ImageStore();
            imageStore.setOriginalName(originalFilename);
            imageStore.setPath(imagePath);
            imageStore = imageStoreService.saveImageStore(imageStore);
            course.setImageStore(imageStore);
        }
        course = courseRepository.saveAndFlush(course);
        return Preconditions.isNotBlank(course);
    }

    public Course saveCourse(Course course) {
        return courseRepository.saveAndFlush(course);
    }

    public PageableResponse<Course> listPageable(PageableRequest pageableRequest){
        PageableResponse<Course> response = new PageableResponse<>();
        Page<Course> page = courseRepository.findByDeletedIsFalse(PageableConverter.toPageRequest(pageableRequest));
        response.setList(page.getContent());
        response.setTotalCount(page.getTotalElements());
        response.setTotalPages(page.getTotalPages());
        response.setCurPage(pageableRequest.getPageNumber() + 1);
        return response;
    }


    public List<Course> findCourseListByCourseType(CourseType courseType) {
        return courseRepository.findByCourseTypeAndDeletedIsFalse(courseType);
    }
}